/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryUserService;

/**
 * 单表生成Controller
 * @author ThinkGem
 * @version 2018-09-08
 */
@Controller
@RequestMapping(value = "${adminPath}/cloudfactory/cloudfactoryUser")
public class CloudfactoryUserController extends BaseController {

	@Autowired
	private CloudfactoryUserService cloudfactoryUserService;
	
	@ModelAttribute
	public CloudfactoryUser get(@RequestParam(required=false) String id) {
		CloudfactoryUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = cloudfactoryUserService.get(id);
		}
		if (entity == null){
			entity = new CloudfactoryUser();
		}
		return entity;
	}
	
	@RequiresPermissions("cloudfactory:cloudfactoryUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(CloudfactoryUser cloudfactoryUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CloudfactoryUser> page = cloudfactoryUserService.findPage(new Page<CloudfactoryUser>(request, response), cloudfactoryUser); 
		model.addAttribute("page", page);
		return "cloudfactory/cloudfactoryUserList";
	}

	@RequiresPermissions("cloudfactory:cloudfactoryUser:view")
	@RequestMapping(value = "form")
	public String form(CloudfactoryUser cloudfactoryUser, Model model) {
		model.addAttribute("cloudfactoryUser", cloudfactoryUser);
		return "cloudfactory/cloudfactoryUserForm";
	}

	@RequiresPermissions("cloudfactory:cloudfactoryUser:edit")
	@RequestMapping(value = "save")
	public String save(CloudfactoryUser cloudfactoryUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, cloudfactoryUser)){
			return form(cloudfactoryUser, model);
		}
		cloudfactoryUserService.save(cloudfactoryUser);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactoryUser/?repage";
	}
	
	@RequiresPermissions("cloudfactory:cloudfactoryUser:edit")
	@RequestMapping(value = "delete")
	public String delete(CloudfactoryUser cloudfactoryUser, RedirectAttributes redirectAttributes) {
		cloudfactoryUserService.delete(cloudfactoryUser);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactoryUser/?repage";
	}
	@RequiresPermissions("cloudfactory:cloudfactoryUser:edit")
	@RequestMapping(value = "audit")
	public String audit(CloudfactoryUser cloudfactoryUser, RedirectAttributes redirectAttributes) {
		int i = cloudfactoryUserService.audit(cloudfactoryUser);
		addMessage(redirectAttributes, "审核成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactoryUser/?repage";
	}
}